package com.mew.chess.core.game.shbz.game;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;


@Data
public class ShbzRoomManager {

    private int gameId;
    private int roomId;
    private int roomType;
    private int[] bets;
    private final ConcurrentMap<Integer, ShbzTableManager> tableMap = new ConcurrentHashMap<>(16);

    public ShbzRoomManager(int gameId, int roomId, int roomType) {
        this.gameId = gameId;
        this.roomId = roomId;
        this.roomType = roomType;
    }

    public int getTableSize(){
        return tableMap.size();
    }

    public void pushTable(ShbzTableManager tableController){
        tableMap.put(tableController.getTableId(), tableController);
    }

    public void removeTable(ShbzTableManager tableController){
        tableMap.put(tableController.getTableId(), tableController);
    }

    public ShbzTableManager getTable(Integer tableId){
        return tableMap.get(tableId);
    }

    public ShbzTableManager getTableByUserId(Long userId){
        if (tableMap.isEmpty()){
            return null;
        }
        return tableMap.values().stream().filter(table -> null != table && null != table.getOnlineUserMap().get(userId)).findFirst().orElse(null);
    }

    public List<ShbzTableManager> findPlayerTable(){
        return tableMap.values().stream().filter(table -> !table.getOnlineUserMap().isEmpty()).collect(Collectors.toList());
    }

    public List<Integer> emptyTableId(){
        List<Integer> list = new ArrayList<>(getTableSize());
        if (tableMap.isEmpty()){
            return list;
        }

        tableMap.values().forEach(table -> {
            if (table.emptyTable()){
                list.add(table.getTableId());
            }
        });
        return list;
    }


}

